package com.homeproject.trailservice.service;

import com.google.common.util.concurrent.ListenableFuture;
import com.homeproject.trailservice.dal.*;
import com.homeproject.trailservice.domain.Customer;
import com.homeproject.trailservice.domain.DeliveryCompany;
import com.homeproject.trailservice.domain.SubOrder;
import com.homeproject.trailservice.domain.relationship.Submit;
import com.homeproject.trailservice.grpc.grouppurchasepkg.GroupPurchaseServiceGrpc;
import com.homeproject.trailservice.grpc.grouppurchasepkg.PurchaseRequest;
import com.homeproject.trailservice.grpc.grouppurchasepkg.PurchaseResponse;
import com.homeproject.trailservice.service.grouppurchase.DeliveryCompanyManager;
import io.grpc.ManagedChannelBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

@Service
@Path("hello")
public class HelloService {

    @GET
    @Produces("text/plain")
    public String hello() {
        return "Hello from Spring";
    }


    ManagedChannelBuilder channelBuilder = ManagedChannelBuilder.forAddress("127.0.0.1",9090).usePlaintext();
    GroupPurchaseServiceGrpc.GroupPurchaseServiceFutureStub stub = GroupPurchaseServiceGrpc.newFutureStub(channelBuilder.build());

    @GET
    @Path("testGrpc")
    @Produces("text/plain")
    public String submitForGroupPurchase() throws ExecutionException, InterruptedException {
        ListenableFuture<PurchaseResponse> future= stub.submitAPurchase(PurchaseRequest.newBuilder()
                .addAllItems(Arrays.asList("melon"))
                .setUserName("abc").build());
        String response = future.get().getPurchaseResult();
        return response;
    }

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private CustomerRepository customerRepository;

    @GET
    @Path("namesFromGraph")
    @Produces("application/json")
    public List<String> getListOfNamesFromGraphDB(){

        List<User> users = userRepository.findAll();

        return users.stream().map(User::getName).collect(Collectors.toList());
    }

    @Autowired
    private SubOrderRepository subOrderRepository;

    @Autowired
    private SubmitRepository submitRepository;

    @Autowired
    private DeliveryCompanyManager deliveryCompanyManager;


    @GET
    @Path("deliverer/deliver_company")
    @Produces("application/json")
    public Response findDeliveryCompanyByDeliverer(@QueryParam("delivererName") String delivererName){
        return Response.ok()
                .entity(deliveryCompanyManager.findDeliveryCompanyByDeliverer(delivererName).get())
                .build();
    }

    @POST
    @Path("submitOrder")
    @Produces("application/json")
    public String submitOrder(Map<String,String> request){
        String customerName = request.get("customerName");
        int suborderPrice = Integer.parseInt(request.get("oderPrice"));

        List<Object> result =customerRepository.submitOrder(customerName,suborderPrice);

        return "success";
    }

}
